from django.shortcuts import render
from rest_framework.response import Response
from users import serializers, models
from rest_framework import viewsets
from rest_framework.decorators import action


# Create your views here.
class User(viewsets.ModelViewSet):
    queryset = models.User.objects.all()
    serializer_class = serializers.UserSer

    @action(detail=False, methods=["POST"])
    def login(self, request):
        phone = request.data.get("phone")
        password = request.data.get("password")
        data = models.User.objects.filter(phone=phone).first()
        if data:
            data1 = models.User.objects.filter(phone=phone, password=password).first()
            if data1:
                return Response({"msg": "登录成功", "status": 200})
            else:
                return Response({"msg": "登录失败", "status": 400})
        else:
            return Response({"msg": "账号不在", "status": 404})
